package com.wasion.pwportable.protocol.p2;

/**
 * 
 * @ClassName: Iec104Parser
 * @Description: IEC 104规约解析
 * @author 高意
 * @date Apr 26, 2016 2:58:55 PM
 * 
 */
public class Iec104Parser {

	// 控制域长度
	protected final static FrameFieldsLength.ControlFieldLengthEnum CONTROL_FIELD_LENGTH = FrameFieldsLength.ControlFieldLengthEnum.LENGTH_CONTROL_FIELD_4;

	// 链路地址长度
	protected final static FrameFieldsLength.LinkAddrLengthEnum LINK_ADDR_LENGTH = FrameFieldsLength.LinkAddrLengthEnum.LENGTH_LINK_ADDR_2;

	// ASDU地址长度
	protected final static FrameFieldsLength.AsduPublicAddrLengthEnum ASDU_ADDR_LENGTH = FrameFieldsLength.AsduPublicAddrLengthEnum.LENGTH_ASDU_ADDR_2;

	// 传输原因长度
	protected final static FrameFieldsLength.COTLengthEnum COT_LENGTH = FrameFieldsLength.COTLengthEnum.LENGTH_COT_2;

	// 信息对象地址长度
	protected final static FrameFieldsLength.InfoObjectAddrLengthEnum INFO_OBJ_ADDR_LENGTH = FrameFieldsLength.InfoObjectAddrLengthEnum.LENGTH_INFO_ADDR_3;

	// 固定帧对象
	private FixedFrame fixedFrame;

	// 可变帧对象
	private VariableFrame variableFrame;

	public FixedFrame getFixedFrame() {
		return fixedFrame;
	}

	public VariableFrame getVariableFrame() {
		return variableFrame;
	}

	/**
	 * 构造方法
	 */
	public Iec104Parser() {
	}

	/**
	 * 数据解析
	 * 
	 * @description 标准104报文数据解析
	 * @param data
	 * @param equip_addr
	 */
	public Iec104Parser(byte[] data) {

		try {
			if (10 == data[0]) {// 固定帧长帧
				fixedFrame = new FixedFrame(data, LINK_ADDR_LENGTH);
			} else { // 可变帧长帧
 				variableFrame = new VariableFrame(data,
						ProtocolEnum.TypeEnum.IEC_104, CONTROL_FIELD_LENGTH,
						LINK_ADDR_LENGTH, ASDU_ADDR_LENGTH, COT_LENGTH,
						INFO_OBJ_ADDR_LENGTH);
			}
		} catch (Exception e) {
			System.err.println("IEC 104报文解析发生异常! >>>> ");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		byte[] data = { (byte) 0x68, (byte) 0x15, (byte) 0x02, (byte) 0x00,
				(byte) 0x00, (byte) 0x00, (byte) 0x1e, (byte) 0x01,
				(byte) 0x03, (byte) 0x00, (byte) 0x01, (byte) 0x00,
				(byte) 0xf1, (byte) 0x01, (byte) 0x00, (byte) 0x01,
				(byte) 0x24, (byte) 0xa8, (byte) 0x05, (byte) 0x09,
				(byte) 0x8f, (byte) 0x06, (byte) 0x11 };
		new Iec104Parser(data);
	}

}
